﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using facebook;
using facebook.Schema;
using FacebookApp;
using System.Data;

namespace FacebookApp
{
    public partial class Admins_PostFriendWall : System.Web.UI.Page
    {
        private static DataTable friends;// = new List<fbuser>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                friends = new DataTable();
                friends.Columns.Add("Id");
                friends.Columns.Add("pic_small");
                friends.Columns.Add("name");

                int countlimit = 0;
                IList<facebook.Schema.user> fr = Master.API.friends.getUserObjects();
                for (int i = 0; i < fr.Count; i++)
                {
                    //WebMsgBox.Show(fr[0].is_app_user.ToString());
                    if (fr[i].is_app_user.ToString() == "True")
                    {
                        // WebMsgBox.Show(fr[0].name.ToString() + fr[i].pic_small);
                        //fbuser u = new fbuser();
                        //u.Id = fr[i].uid.ToString();
                        //u.name = fr[i].name;
                        //u.pic_small = fr[i].pic_small;
                        //friends.Add(u);
                        AddRow(fr[i].uid.ToString(), fr[i].pic_big, fr[i].name);
                        countlimit++;
                    }
                    if (countlimit == 20)
                        break;
                }
                BindGrid();
                //WebMsgBox.Show(fr[0].is_app_user.ToString());
            }
        }

        protected void grdEbook_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdEbook.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut",
                                              "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick",
                                                             "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex +
                                                             ")");
                    }
                }
            }
        }

        protected void grdEbook_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdEbook.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        private void BindGrid()
        {
            grdEbook.DataSource = friends;
            grdEbook.DataBind();
        }

        protected void grdEbook_ItemCommand(object source, DataGridCommandEventArgs e)
        { }

        protected void AddRow(string id, string pic_small, string name)
        {
            DataRow rows = friends.NewRow();
            rows["Id"] = id;
            rows["pic_small"] = pic_small;
            rows["name"] = name;

            friends.Rows.Add(rows);
        }
        protected void btpost_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdEbook.Items.Count; i++)
                {
                    item = grdEbook.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                        {
                            string strId = item.Cells[1].Text;
                            try
                            {
                                posttowall(strId, "Sản phẩm mới", "name", "mô tả", "15", "1", "Khoa học", "1233");
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                grdEbook.CurrentPageIndex = 0;
                BindGrid();
            }
            catch
            {
            }
        }

        protected void posttowall(string uid, string _header, string _nameproduct, string _description, string id, string group_id, string group_name, string pathimage)
        {
            string headerlinkapp = ConfigurationSettings.AppSettings["fb_headerpath"].ToString();
            string domain = ConfigurationSettings.AppSettings["fb_domain"].ToString();
            string header = _header;//"Sản phẩm mới";
            string _link = headerlinkapp + "Pages/ProductDetail.aspx?Category_Id=" + group_id + "&Id=" + id + "";
            attachment attachment = new attachment();

            attachment.caption = headerlinkapp;
            attachment.name = _nameproduct;// "Tên sản phẩm";
            attachment.href = _link;
            attachment.description = _description;// "Mô tả sản phẩm";
            attachment.properties = new attachment_property()
            {
                category = new attachment_category()
                {
                    href = headerlinkapp + "Pages/ListGroupProduct.aspx?Id=" + group_id + "&GroupName=" + group_name.Replace(" ", "_") + "",
                    text = group_name//"humor"
                }
                //,
                //ratings = "5 stars"
            };
            attachment.media = new List<attachment_media>(){new attachment_media_image()
                                    {                                        
                                        src = domain + pathimage,//"http://fbapps.somee.com/Styles/Images/Koala.jpg",
                                        href =_link//"http://icanhascheezburger.com/2009/04/22/funny-pictures-bursting-with-joy/"
                                    }};
            List<action_link> action_link = new List<action_link>() { new action_link() { 
                text = "Make by your way",
                href = headerlinkapp } };
            WebMsgBox.Show(pathimage);
            Master.API.stream.publish(header, attachment, action_link, uid, 1814236399);
            //Master.API.notifications.send(uid, "New", "user_to_user");
        }
    }
}